<template>
    <div class="menu-layout">
        <el-container class="layout-container">
            <!-- 顶部导航 -->
            <el-header height="60px" class="main-header">
                <div class="header-nav">
                    <div class="nav-left">
                        <span class="company-name">公司名称:爱吃兔</span>
                        <div class="nav-tabs">
                            <div class="nav-tab" :class="{ 'active-tab': activeNav === '集团管理' }"
                                @click="activeNav = '集团管理' ">集团管理</div>
                            <div class="nav-tab" :class="{ 'active-tab': activeNav === '企业管理' }"
                                @click="activeNav = '企业管理'">企业管理</div>
                            <div class="nav-tab" :class="{ 'active-tab': activeNav === '平台运营' }"
                                @click="activeNav = '平台运营'">平台运营</div>
                            <div class="nav-tab" :class="{ 'active-tab': activeNav === '系统设置' }"
                                @click="activeNav = '系统设置'">系统设置</div>
                        </div>
                    </div>
                    <div class="nav-right">
                        <div class="search-box">
                            <el-input v-model="searchText" placeholder="搜索" :prefix-icon="Search"
                                class="search-input"></el-input>
                        </div>
                        <div class="nav-icons">
                            <el-tooltip content="应用市场" placement="bottom">
                                <span class="nav-icon">应用市场</span>
                            </el-tooltip>
                            <el-tooltip content="客户端" placement="bottom">
                                <span class="nav-icon">客户端</span>
                            </el-tooltip>
                            <el-tooltip content="通知" placement="bottom">
                                <el-badge :value="3" class="nav-badge">
                                    <el-icon class="nav-icon-item">
                                        <Bell />
                                    </el-icon>
                                </el-badge>
                            </el-tooltip>
                            <el-tooltip content="菜单" placement="bottom">
                                <el-icon class="nav-icon-item">
                                    <Menu />
                                </el-icon>
                            </el-tooltip>
                            <el-dropdown @command="handleCommand" trigger="click">
                                <span class="user-dropdown">
                                    <el-avatar :size="30" :icon="UserFilled" class="user-avatar" />
                                    <span class="username">{{ username }}</span>
                                </span>
                                <template #dropdown>
                                    <el-dropdown-menu>
                                        <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                                        <el-dropdown-item command="settings">设置</el-dropdown-item>
                                        <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
                                    </el-dropdown-menu>
                                </template>
                            </el-dropdown>
                        </div>
                    </div>
                </div>
            </el-header>
            <el-container class="content-container">
                <!-- 左侧菜单 -->
                <el-aside width="220px" class="aside-menu">
                    <div class="menu-header">
                        <span>爱吃兔溯源管理</span>
                    </div>
                    <el-menu default-active="1-4-1" class="el-menu-vertical" background-color="#f0f2f5"
                        text-color="#333" active-text-color="#409EFF" router>
                        <el-sub-menu index="1-4">
                            <template #title>溯源管理</template>
                            <el-menu-item index="/menu/traceability">溯源数据</el-menu-item>
                        </el-sub-menu>
                    </el-menu>
                </el-aside>
                <!-- 右侧内容区 -->
                <el-main class="content-main">
                    <RouterView />
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Delete, Edit, Plus, Search, Bell, Menu, UserFilled } from '@element-plus/icons-vue'
import { RouterLink, RouterView, useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { userService } from '@/http/userService'
import HelloWorld from '../components/HelloWorld.vue'

const router = useRouter()
const activeNav = ref('企业管理')
const searchText = ref('')
const darkMode = ref(false)
const username = ref('管理员')

// 处理下拉菜单命令
const handleCommand = (command: string) => {
    switch (command) {
        case 'logout':
            handleLogout()
            break
        case 'profile':
            // 处理个人信息
            break
        case 'settings':
            // 处理设置
            break
    }
}

// 处理退出登录
const handleLogout = () => {
    ElMessageBox.confirm('确定要退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        // 调用登出方法
        userService.logout()
        // 跳转到登录页面
        router.push('/login')
    }).catch(() => {
        // 取消操作
    })
}

// 格式化百分比显示
const percentFormat = (percentage: number) => {
    return `${percentage}%`
}
</script>

<style scoped>
.menu-layout {
    height: 100vh;
    width: 100%;
}

.layout-container {
    height: 100%;
    display: flex;
    flex-direction: column;
}

.content-container {
    flex: 1;
    display: flex;
}

/* 顶部导航样式 */
.main-header {
    background-color: #fff;
    color: #333;
    padding: 0;
    line-height: 60px;
    border-bottom: 1px solid #e0e0e0;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.header-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100%;
    padding: 0;
}

.nav-left {
    display: flex;
    align-items: center;
    height: 100%;
}

.company-name {
    padding: 0 20px;
    font-weight: 500;
    font-size: 14px;
    color: #333;
}

.nav-tabs {
    display: flex;
    height: 100%;
}

.nav-tab {
    padding: 0 20px;
    cursor: pointer;
    height: 100%;
    display: flex;
    align-items: center;
    font-size: 14px;
    transition: all 0.3s;
    position: relative;
}

.nav-tab:hover {
    color: #409EFF;
    background-color: #f5f7fa;
}

.active-tab {
    color: #409EFF;
    background-color: #f5f7fa;
}

.active-tab::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 2px;
    background-color: #409EFF;
}

.nav-right {
    display: flex;
    align-items: center;
    height: 100%;
}

.search-box {
    margin-right: 20px;
}

.search-input {
    width: 200px;
}

.nav-icons {
    display: flex;
    align-items: center;
    height: 100%;
}

.nav-icon {
    padding: 0 15px;
    font-size: 14px;
    cursor: pointer;
    color: #666;
}

.nav-icon:hover {
    color: #409EFF;
}

.nav-icon-item {
    font-size: 20px;
    padding: 0 15px;
    cursor: pointer;
    color: #666;
}

.nav-icon-item:hover {
    color: #409EFF;
}

.nav-badge {
    margin: 0 5px;
}

.user-dropdown {
    display: flex;
    align-items: center;
    cursor: pointer;
    padding: 0 15px;
    height: 100%;
}

.user-dropdown:hover {
    background-color: #f5f7fa;
}

.user-avatar {
    background-color: #409EFF;
}

.username {
    margin-left: 8px;
    font-size: 14px;
}

/* 左侧菜单样式 */
.aside-menu {
    background-color: #f0f2f5;
    border-right: 1px solid #e0e0e0;
    overflow-x: hidden;
    height: 100%;
}

.menu-header {
    height: 50px;
    line-height: 50px;
    padding: 0 15px;
    font-weight: bold;
    border-bottom: 1px solid #e0e0e0;
    background-color: #f0f2f5;
}

.el-menu-vertical {
    border-right: none;
}

.active-menu-item {
    background-color: #e6f1fc !important;
    color: #409EFF !important;
    border-left: 4px solid #409EFF;
}

/* 右侧内容区样式 */
.content-main {
    padding: 15px;
    background-color: #f5f7fa;
    height: 100%;
    overflow-y: auto;
}

.content-header {
    margin-bottom: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.content-title {
    font-size: 16px;
    font-weight: bold;
    display: flex;
    align-items: center;
    gap: 10px;
}

/* 分页样式 */
.pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
}

.total-info {
    margin-right: 15px;
}
</style>